var ZtreeObj;

function getSetting(isOnlyClickLeafNode){
    var setting = {
        data: {
            simpleData: {
                enable: true
            }
        },
        edit: {
            enable: false
        },
        /*因为是第一次全部加载树节点，所以不用设置此处
       async: {
           enable: true,
           url: function (treeId, treeNode) {
               return getContextPath() + '/sys/org/list/listAllTree?parentId=' + treeNode.id;
           }
       },*/
        callback: {
            onClick: function (event, treeId, treeNode) {
                orgTreeClickCallback(treeNode);
            }/*,
           beforeClick: function (treeId, treeNode) {
               if(isOnlyClickLeafNode && treeNode.type != '3'){
                   return false;
               }else {
                   return true;
               }
           }*/
        }
    };
    return setting;
};

function filter(node) {
    return (node.type == 3); //过滤出党支部
    // return (!node.children); //过滤出叶子节点
}

/**
 * 创建树结构
 * @param isQueryTop 是否只能单击叶子节点
 * @param isOnlyClickLeafNode 是否只能单击叶子节点
 * @param paramUrl 请求的路由
 */
function createOrgTree(isQueryTop,paramUrl,isOnlyClickLeafNode){
    var url= getContextPath() + paramUrl+ "?queryTop="+isQueryTop;
    $.ajax({
        url: url,
        type: "post",
        async: false,
        dataType: "text",
        success: function (data) {
            var zNodes = eval("(" + data + ")");
            ZtreeObj = $.fn.zTree.init($("#treeRegion"), getSetting(isOnlyClickLeafNode), zNodes);
            // 第一个节点高亮
            //$('#treeRegion').find('#treeRegion_1_a').trigger('click');
        },
        error: function (XMLHttpRequest, textStatus) {
            layer.msg(textStatus);
        }
    });
}

function getCurrentSelectOrgNodeId(){
    var nodes= ZtreeObj.getSelectedNodes();
    if(nodes.length>0){
        return nodes[0].id;
    }
    return "";
}
